using UnityEngine;
using System.Collections.Generic;
using WyrmTale;
#if UNITY_EDITOR
using UnityEditor;
using System.IO;
#endif

public enum InteractiveType
{
	Player,
	Zombie,
	Key,
	Remedy,
	Shield,
	Weapon,
	Container,
	COUNT,
}

public class InteractiveManager : Singleton<InteractiveManager>
{
	private Dictionary<InteractiveType, List<InteractiveInfo>> interactives = new Dictionary<InteractiveType, List<InteractiveInfo>>();

	private void Awake()
	{
		LoadAllAssets(ref interactives);
	}

	public InteractiveInfo GetInfo(string _itemName)
	{
		foreach (KeyValuePair<InteractiveType, List<InteractiveInfo>> kvp in interactives)
		{
			foreach (InteractiveInfo info in kvp.Value)
			{
				if (info.itemName == _itemName)
				{
					return info;
				}
			}
		}

		return null;
	}

	public void LoadAllAssets(ref Dictionary<InteractiveType, List<InteractiveInfo>> _assets)
	{
		for (int i = 0; i < (int)InteractiveType.COUNT; i++)
		{
			InteractiveInfo[] infos = Resources.LoadAll<InteractiveInfo>("Stats/" + ((InteractiveType)i).ToString() + "s");
			foreach (InteractiveInfo info in infos)
			{
				if (!_assets.ContainsKey((InteractiveType)i))
				{
					_assets.Add((InteractiveType)i, new List<InteractiveInfo>());
				}
				
				if (!_assets[(InteractiveType)i].Contains(info))
				{
					_assets[(InteractiveType)i].Add(info);
				}
			}
		}
	}
	
#if UNITY_EDITOR
	public static void SaveAsset(InteractiveInfo _info, string _typeName, string _assetName)
	{
		if (string.IsNullOrEmpty(_info.itemName))
		{
			Debug.LogError("Error - Reference Name must not be empty");
			return;
		}

		string folder = "Assets/Resources/Stats/" + _typeName + "s/";
		if (!Directory.Exists(folder))
		{
			AssetDatabase.CreateFolder("Assets/Resources/Stats", _typeName + "s");
		}
		
		if (!AssetDatabase.Contains(_info))
		{
			AssetDatabase.CreateAsset(_info, folder + _assetName + ".asset");
			AssetDatabase.SaveAssets();
			AssetDatabase.Refresh();

			Debug.Log("Saved " + _assetName + " at " + (folder + _assetName + ".asset"));
		}
	}

	public static void DestroyAsset(InteractiveInfo _info)
	{
		string path = AssetDatabase.GetAssetPath(_info);
		Debug.Log("Destroyed " + _info.name + " at " + path);
		AssetDatabase.DeleteAsset(path);
	}
	
	public static void LoadAssetDictionary(ref Dictionary<InteractiveType, List<InteractiveInfo>> _dictionary)
	{
		for (int i = 0; i < (int)InteractiveType.COUNT; i++)
		{
			string folder = "Assets/Resources/Stats/" + ((InteractiveType)i).ToString() + "s";
			if (Directory.Exists(folder))
			{
				string[] objs = AssetDatabase.FindAssets("", new string[] {folder});
				foreach (string guid in objs)
				{
					string path = AssetDatabase.GUIDToAssetPath(guid);
					InteractiveInfo info = AssetDatabase.LoadAssetAtPath(path, typeof(InteractiveInfo)) as InteractiveInfo;
					if (info)
					{
						if (!_dictionary.ContainsKey((InteractiveType)i))
						{
							_dictionary.Add((InteractiveType)i, new List<InteractiveInfo>());
						}
						
						if (!_dictionary[(InteractiveType)i].Contains(info))
						{
							_dictionary[(InteractiveType)i].Add(info);
						}
					}
				}
			}
		}
	}
#endif
}
